package com.chdwmp.product.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.chdwmp.product.bean.PageResult;
import com.chdwmp.product.bean.User;
import com.chdwmp.product.bean.UserListSearch;
import com.chdwmp.product.constant.CommonConstants;
import com.chdwmp.product.constant.Status;
import com.chdwmp.product.exception.CommonException;
import com.chdwmp.product.mapper.UserMapper;
import com.chdwmp.product.po.UserPo;
import com.chdwmp.product.service.RedisService;
import com.chdwmp.product.service.UserService;
import com.chdwmp.product.system.CommonContext;
import com.chdwmp.product.token.MD5Util;
import com.chdwmp.product.vo.LoginUserVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;
import java.util.UUID;

/**
 * @Auther: LLL
 * @Date: 2021/3/6 15:10
 * @Description:
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;
    @Autowired
    private RedisService redisService;
    @Autowired
    CommonContext commonContext;


    @Override
    @Transactional
    public UserPo login(LoginUserVO userVO) {

        int count  = userMapper.queryUserIsExits(userVO.getUsername());
        if(count == 0)
            throw new CommonException(Status.USER_IS_NOT_EXITS);
        User user = userMapper.queryUserPo(userVO.getUsername());
        if(ObjectUtil.isNotNull(user)){
            if(!user.getPassWord().equals(userVO.getPassword())){
                throw new CommonException(Status.ERROR_INCORRECT_USERNAME_OR_PASSWORD);
            }
        }
        UserPo po  = new UserPo();
        // 4. 保存token
        //有效期2小时
        //处理token
        redisService.setString(user.getId(), UUID.randomUUID().toString());
        redisService.setString(redisService.getString(user.getId()),user.getId());
        po.setUserId(user.getId());
        po.setToken(redisService.getString(user.getId()));
        return po;
    }

    @Override
    @Transactional
    public void logout(String token) {
        String id = redisService.getString(token);
        redisService.deleteString(id);
        redisService.deleteString(token);
    }

    @Override
    public void addUser(User user) {
        int count = userMapper.queryUserIsExits(user.getUserName());
        if(count > 0)
            throw new CommonException(Status.USER_IS_NOT_EXITS);
        user.setPassWord(MD5Util.encode("123456"));
        userMapper.insertUser(user);
    }

    @Override
    public void updateUser(User user) {
        user.setPassWord(MD5Util.encode(user.getPassWord()));
        int count  = userMapper.queryUserIsExitsForUpdate(user.getId(),user.getUserName());
        if(count > 0)
            throw new CommonException(Status.USER_IS_NOT_EXITS);
        userMapper.updateUser(user);
    }

    @Override
    public PageResult getUserList(UserListSearch search) {

        int rows = search.getPageSize();
        if (rows == 0) {
            rows = CommonConstants.PAGE_SIZE;
        }
        int start_rows = search.getPage() * rows;
        search.setPage(start_rows);
        search.setPageSize(rows);

        if(StringUtils.isEmpty(search.getStartTime()) || "".equals(search.getStartTime())){
            search.setStartTime(null);
        }
        if(StringUtils.isEmpty(search.getEndTime()) || "".equals(search.getEndTime())){
            search.setEndTime(null);
        }

        int total_count  = userMapper.queryTotalCount(search);
        List<User> userList = userMapper.queryUserList(search);

        boolean isHasMore = true;
        if(rows == 0){
            isHasMore = total_count > (search.getPage() + 1) * CommonConstants.PAGE_SIZE;
        }else {
            isHasMore = total_count > (search.getPage() + 1) * rows;
        }

        if (total_count == 0){
            return new PageResult<>(total_count,isHasMore, Collections.emptyList());
        }
        return new PageResult<>(total_count,isHasMore,userList);
    }

    @Override
    public void deleteUser(String id) {
        if(ObjectUtil.isNull(id))
            throw new CommonException(Status.MISS_REQUIRED_PARAMETER);
        userMapper.deleteUser(id);
    }
}
